<template>
  <div class="innerContainer">
    <common-title>
      <template #expand>
        <el-button type="success" @click="add">新增用户</el-button>
      </template>
    </common-title>

    <div class="innerPane">
      <div class="filterArea">
        <el-form :inline="true" :model="filterForm" class="demo-form-inline">
          <el-form-item label="用户名">
            <el-input @keyup.enter="search" v-model="searchForm.userName" placeholder="请输入用户姓名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button plain type="primary" @click="search">查询</el-button>
            <el-button plain @click="onClear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="tableData" border height="100%" :cell-style="{ textAlign: 'center' }" row-class-name="fixed-row">
        <el-table-column header-align="center" prop="account" label="账号" />
        <el-table-column header-align="center" prop="userName" label="用户姓名" />
        <el-table-column header-align="center" prop="sexText" label="性别" width="80" />
        <el-table-column header-align="center" prop="idNumber" label="身份证号" />
        <el-table-column header-align="center" prop="jobNumber" label="工号" />
        <el-table-column header-align="center" prop="roleNames" label="用户角色" width="180">
          <template #default="scope">
            {{ scope.row.roleNames.join(";") }}
          </template>
        </el-table-column>
         <el-table-column header-align="center" prop="deptNames" label="所属部门" width="180">
          <template #default="scope">
            {{  scope.row.deptNames?.join(";") }}
          </template>
        </el-table-column>

        <el-table-column label="操作" header-align="center" width="220" prop="col9">
          <template #default="scope">
            <el-link plain type="primary" size="small" @click="edit(scope.$index, scope.row)">编辑</el-link>

            <el-link plain type="danger"
              v-if="!scope.row.roleCodes || scope.row.roleCodes.indexOf('super_admin') === -1" size="small"
              @click="del(scope.row.id)">删除</el-link>

          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination style="margin-top: 20px" :current-page="searchForm.current" :page-size="searchForm.size"
        :page-sizes="[10, 20, 30, 40]" layout="->,total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>

  <detail-view ref="viewObj" @confirm="search"></detail-view>
</template>

<script setup>
import CommonTitle from "@/components/CommonTitle.vue";
import { reqUserList, reqUserDel, getConfigParams } from "../../utils/requests";
import { onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";

import DetailView from "@/views/user/view.vue";

const router = useRouter();

let sexList = ref()
// Dom 挂载之后
onMounted(async () => {
  sexList.value = await getParams("sys.user.sex");
  search();
});
const getParams = async (params) => {
  let res = await getConfigParams({
    cats: params,
  });
  return res;
};

// 用户数据
let tableData = ref([]);

let total = ref(0);
// 搜索条件
const searchForm = reactive({
  current: 1,
  size: 10,
  userName: "",
});

const onClear = () => {
  searchForm.userName = "";
  search();
};

const handleSizeChange = (size) => {
  searchForm.size = size;
  search();
};
const handleCurrentChange = (current) => {
  searchForm.current = current;
  search();
};
const search = () => {
  reqUserList(searchForm).then((res) => {
    tableData.value = res.records;

    total.value = res.total;
  });
};

// 详情查看弹窗
const viewObj = ref();

//添加
const add = () => {
  viewObj.value.openDialog(null, sexList);
};

//编辑
const edit = (index, row) => {
  let rowData = Object.assign({ _o: "w" }, row);
  viewObj.value.openDialog(rowData, sexList.value);
};

// 删除用户
const del = (id) => {
  ElMessageBox.confirm("确定要删除该用户信息吗?", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await reqUserDel({ ids: id });
      if (res.data.success) {
        ElMessage.success("删除成功");
        getUserList();
      } else {
        ElMessage.error("删除失败");
      }
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};
</script>

<style scoped>
.filterArea {
  display: flex;
  justify-content: space-between;
  padding-top: 10px;
}

.filterArea .el-select {
  width: 120px;
}

.el-table .el-link {
  margin-right: 10px;
}
</style>
